package com.next.controller;

import com.next.common.JsonData;
import com.next.dto.TrainNumberDetailDto;
import com.next.dto.TrainNumberDto;
import com.next.model.TrainNumber;
import com.next.model.TrainNumberDetail;
import com.next.model.TrainStation;
import com.next.param.TrainNumberDetailParam;
import com.next.service.TrainNumberDetailService;
import com.next.service.TrainNumberService;
import com.next.service.TrainStationService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/admin/train/numberDetail")
public class TrainNumberDetailController {

    @Resource
    private TrainNumberDetailService trainNumberDetailService;
    @Resource
    private TrainStationService trainStationService;
    @Resource
    private TrainNumberService trainNumberService;

    @RequestMapping("/list.page")
    public ModelAndView page() {
        return new ModelAndView("trainNumberDetail");
    }

    @RequestMapping("/list.json")
    @ResponseBody
    public JsonData list() {
        List<TrainNumberDetail> numberDetailList = trainNumberDetailService.getAll();
        List<TrainStation> stationList = trainStationService.getAll();
        List<TrainNumber> numberList = trainNumberService.getAll();
        Map<Integer, String> numberMap = numberList.stream().collect(Collectors.toMap(TrainNumber::getId, TrainNumber::getName));
        Map<Integer, String> stationMap = stationList.stream().collect(Collectors.toMap(TrainStation::getId, TrainStation::getName));
        List<TrainNumberDetailDto> trainNumberDetailDtoList = numberDetailList.stream().map(number -> {
            TrainNumberDetailDto trainNumberDetailDto = new TrainNumberDetailDto();
            BeanUtils.copyProperties(number, trainNumberDetailDto);
            trainNumberDetailDto.setToStation(stationMap.get(stationMap.get(number.getToStationId())));
            trainNumberDetailDto.setFromStation(stationMap.get(stationMap.get(number.getFromStationId())));
            trainNumberDetailDto.setTrainNumber(numberMap.get(trainNumberDetailDto.getTrainNumberId()));
            return trainNumberDetailDto;
        }).collect(Collectors.toList());
        return JsonData.success(trainNumberDetailDtoList);
    }

    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData save(TrainNumberDetailParam param) {
        trainNumberDetailService.save(param);
        return JsonData.success();
    }

    @RequestMapping("/delete.json")
    @ResponseBody
    public JsonData delete(@RequestParam Integer id) {
        trainNumberDetailService.delete(id);
        return JsonData.success();
    }
}
